package moo.stargalaxy_bukkit.config;

import com.alibaba.fastjson.JSONObject;
import moo.stargalaxy_bukkit.StarGalaxy_Bukkit;
import moo.stargalaxy_bukkit.api.GalaxyAPI;
import moo.stargalaxy_bukkit.api.GalaxyBukkitAPI;
import moo.stargalaxy_bukkit.data.BukkitStarGalaxyData;
import moo.stargalaxy_bukkit.data.ServerData;
import org.bukkit.Bukkit;
import org.bukkit.configuration.file.YamlConfiguration;


import java.io.File;
import java.util.*;

import static io.netty.util.internal.PlatformDependent.getObject;

public class Config {
    private static Map<String, ServerData> serverDataMap = new LinkedHashMap<>();
    private static List<String> serverList = new ArrayList<>();
    private static YamlConfiguration configuration;
    private static int time;
    private static String fid;
    public static String version = "1.0.1";

    public static void loadYaml(){
        File file = new File(StarGalaxy_Bukkit.getPlugin().getDataFolder(), "config.yml");
        if(!file.exists()){
            StarGalaxy_Bukkit.getPlugin().saveResource("config.yml", false);
        }
        configuration = YamlConfiguration.loadConfiguration(file);
        for (String commandSimpleCmd : configuration.getConfigurationSection("Command_SimpleCmd").getKeys(false)) {
            BukkitStarGalaxyData.getQuickCmd().put(
                    configuration.getString(commandSimpleCmd + ".SimpleCMD"),
                    configuration.getString(commandSimpleCmd + ".RawCommand"));
        }
        // 获取设置跨服时候延迟等待时间
        time = configuration.getInt("Server_Set.Cross_Delaytime");
        // Bukkit.getConsoleSender().sendMessage("§2[StarGalaxy_Bukkit]: 加载了" + BukkitStarGalaxyData.getQuickCmd().size() + "条指令");
    }

    public static void loadData(){
        String string = Config.getConfiguration().getString("Server_Set.Server_Fid").toUpperCase();
        if(string.equals("LOGIN") || string.equals("LOBBY")){
            // 作为大厅服直接关闭定时循环提交数据
            Config.getConfiguration().set("Server_Set.Updata_RunSataus", false);
            return;
        }
        if(string.length() == 0){
            System.out.println("StarGalaxy - 服务器识别码设置为空 (视作大厅功能性插件运行)");
            fid = "-";
            // 作为大厅服直接关闭定时循环提交数据
            Config.getConfiguration().set("Server_Set.Updata_RunSataus", false);
            Config.getConfiguration().set("Server_Set.PAPI_Enable", false);
            return;
        }
        // 不从 server.properties 进行读取FID，实在无用这个
        JSONObject jsonObject = null;
        //        if(!Bukkit.getServerName().contains(" ")){
        //            jsonObject = GalaxyAPI.getServerInfo(Bukkit.getServerName());
        //        }
        //        fid = Bukkit.getServerName();
        if(jsonObject == null || !jsonObject.containsKey("datas")) {
            jsonObject = GalaxyAPI.getServerInfo(getConfiguration().getString("Server_Set.Server_Fid"));
            fid = getConfiguration().getString("Server_Set.Server_Fid");
            if(!jsonObject.containsKey("datas")){
                fid = "-";
                // 作为大厅服直接关闭定时循环提交数据
                Config.getConfiguration().set("Server_Set.Updata_RunSataus", false);
                return;
            }
        }
        String ownerUUID = (String) jsonObject.getJSONObject("datas").getObject("servers", Map.class).get("玩家识别码");
        GalaxyAPI.getPlayerServer(ownerUUID).getJSONObject("datas").getJSONObject("servers").getInnerMap().forEach((key, value) -> {
            ServerData serverData = new ServerData((String) ((Map) value).get("显示名称"),
                    (String) ((Map) value).get("玩家名称"),
                    (String) ((Map) value).get("玩家识别码"),
                    (String) ((Map) value).get("创建时间"),
                    String.valueOf((int) ((Map) value).get("在线人数")),
                    String.valueOf((int) ((Map) value).get("最大人数")),
                    (String) ((Map) value).get("状态文本")
            );
            serverDataMap.put(key, serverData);
        });
        serverList = new ArrayList<>(serverDataMap.keySet());
    }

    public static String getFid() {
        return fid;
    }

    public static void setFid(String fid) {
        Config.fid = fid;
    }

    public static List<String> getServerList() {
        return serverList;
    }

    public static void setServerList(List<String> serverList) {
        Config.serverList = serverList;
    }

    public static YamlConfiguration getConfiguration() {
        return configuration;
    }

    public static void setConfiguration(YamlConfiguration configuration) {
        Config.configuration = configuration;
    }

    public static int getTime() {
        return time;
    }

    public static void setTime(int time) {
        Config.time = time;
    }

    public static Map<String, ServerData> getServerDataMap() {
        return serverDataMap;
    }

    public void setServerDataMap(Map<String, ServerData> serverDataMap) {
        Config.serverDataMap = serverDataMap;
    }
}
